package cn.darkfire.web.Controller;

import cn.darkfire.web.POJO.Status;
import cn.darkfire.web.Service.UserService;
import cn.darkfire.web.Service.impl.UserServiceImpl;
import cn.darkfire.web.Utils.DataUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/sendEmail.do")
public class SendEmailController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        UserService service  = new UserServiceImpl();
        Status status = service.sendEmail(req.getParameter("username"),req.getParameter("email"));
        if(status.getCode()==-1){
            resp.getWriter().println(status.getTips());
        }else{
            //将用户名和验证码放在一起,设置为token,当用户验证的时候,取验证码和用户名加密进行验证是否正确。
            req.getSession().setAttribute("token", DataUtils.getInstance().stringToMD5(req.getParameter("username")+status.getTips()));
            resp.getWriter().println("验证码发送成功！");
        }
    }
}
